/**
 * Copyright (C) 2009+ Spark Zheng
 *
 * @file        test/test_phpserialize.c 
 * @date        2010-06-01
 * @brief	
 * @see         dlib
 * @history
 */

#include <stdio.h>
#include <string.h>

#include "serialize.h"

#ifndef BUF_LEN
#define BUF_LEN	1024
#endif
static char buf[BUF_LEN];
static char output[BUF_LEN];
//static char *buf;

int main(int argc, char **argv)
{
	int ret;

	memset(buf, 0, BUF_LEN);
	memset(output, 0, BUF_LEN);

//////////////////////////////////////////////////////////////////////test unserialize
	char *serialize1 = 
		"a:3:{s:3:\"cmd\";s:5:\"flush\";s:4:\"type\";s:5:\"black\";s:4:\"test\";a:2:{s:1:\"a\";i:1;i:0;i:2;}}";
	char *serialize2 = "i:1;";
	char *serialize3 = "d:19.232342;";
	char *serialize4 = "s:9:\"just test\";";

	char *serialize_error = "d:19.232342";

	dlib_alloc_t *na = NULL;
	na = dlib_alloc_init(buf, BUF_LEN);

	if (na == NULL) {
		fprintf(stderr, "allocator error.\n");
		return -1;
	} else {
		fprintf(stdout, "INFO na[%p].\n", buf);
	}

	dlib_php_map_t *map1;
	//ret = php_decode_map(serialize1, strlen(serialize1), &map1, na);
	ret = php_unserialize(serialize1, strlen(serialize1), &map1, na);
	if (ret <= 0) {
		fprintf(stderr, "unserialize %s error[%d].\n", serialize1, ret);
		return -2;
	}
	dlib_php_string_t cmd_key = {
		3, "cmd"
	};
	dlib_php_string_t *cmd_val = NULL;
	ret = map_get_string_by_string(map1, &cmd_key, &cmd_val);
	if (ret < 0) {
		fprintf(stderr, "cmd => val error[%d].\n", ret);
	} else {
		char *cmd = strndup(cmd_val->val, cmd_val->len);
		fprintf(stdout, "cmd => %s[%d].\n", cmd, cmd_val->len);
		free(cmd);
	}

	dlib_php_string_t test_key = {
		4, "test"
	};
	dlib_php_map_t *test_val_map = NULL;
	/*
	dlib_php_mappair_t *pair = NULL;
	if (php_map_get(map1, DLIB_PHP_STRING, (void *)(&test_key), &pair) == 0) {
		test_val_map = (dlib_php_map_t *)pair->val_ptr;
	}
	*/
	map_get_map_by_string(map1, &test_key, &test_val_map);

	int test_index0_val = 0;
	ret = map_get_int_by_index(test_val_map, 0, &test_index0_val);
	if (ret < 0) {
		fprintf(stderr, "[map test]0 => val error[%d].\n", ret);
	} else {
		fprintf(stdout, "[map test]0 => %d.\n", test_index0_val);
	}

	/*
	ret = map_get_string_by_string(map1, &test_key, &test_val);
	if (ret < 0) {
		fprintf(stderr, "test => val error[%d].\n", ret);
	} else {
		char *test_str = strndup(test_val->val, test_val->len);
		fprintf(stdout, "cmd => %s[%d][%s].\n", test_str, test_val->len, test_val->val);
		free(test_str);
	}
	*/

	int int_val = 0;
	ret = php_decode_int(serialize2, strlen(serialize2), &int_val, na);
	if (ret <= 0) {
		fprintf(stderr, "[unserialize int] error[%d].\n", ret);
	} else {
		fprintf(stdout, "[unserialize int] %d.\n", int_val);
	}
	
	dlib_php_double_t double_val;
	ret = php_decode_double(serialize3, strlen(serialize3), &double_val, na);
	if (ret <= 0) {
		fprintf(stderr, "[unserialize double] error[%d].\n", ret);
	} else {
		fprintf(stdout, "[unserialize double] %lf.\n", double_val.val);
	}

	dlib_php_string_t str_val;
	ret = php_decode_string(serialize4, strlen(serialize4), &str_val, na);
	if (ret <= 0) {
		fprintf(stderr, "[unserialize string] error[%d].\n", ret);
	} else {
		char *str_real = strndup(str_val.val, str_val.len);
		fprintf(stdout, "[unserialize string] %s[%s][%d].\n", str_real, str_val.val, str_val.len);
		free(str_real);
	}

	/* error */
	ret = php_decode_double(serialize_error, strlen(serialize_error), &double_val, na);
	if (ret <= 0) {
		fprintf(stderr, "[unserialize double] error[%d].\n", ret);
	} else {
		fprintf(stdout, "[unserialize double] %lf.\n", double_val.val);
	}

///////////////////////////////////////////////////////////////////////////////////test unserialize end

///////////////////////////////////////////////////////////////////////////////////test serialize
	/* format the string:
		"a:3:{s:3:\"cmd\";s:5:\"flush\";s:4:\"type\";s:5:\"black\";s:4:\"test\";a:2:{s:1:\"a\";i:1;i:0;i:2;}}";
	*/
	dlib_php_map_t *out_map = php_alloc_map(na, 3);
	dlib_php_string_t *cmd_out_key = php_alloc_string(na, 3, "cmd");
	dlib_php_string_t *cmd_out_val = php_alloc_string(na, 5, "flush");
	dlib_php_string_t *type_out_key = php_alloc_string(na, 4, "type");
	dlib_php_string_t *type_out_val = php_alloc_string(na, 5, "black");
	dlib_php_string_t *test_out_key = php_alloc_string(na, 4, "test");
	dlib_php_map_t *test_out_map = php_alloc_map(na, 2);

	dlib_php_string_t *test_a_out_key = php_alloc_string(na, 1, "a");
	map_set_int_by_string(&test_out_map, na, test_a_out_key, 1);
	map_set_int_by_index(&test_out_map, na, 0, 1);
	map_set_string_by_string(&out_map, na, cmd_out_key, cmd_out_val);
	map_set_string_by_string(&out_map, na, type_out_key, type_out_val);
	map_set_map_by_string(&out_map, na, test_out_key, test_out_map);
	
	ret = php_encode_map(out_map, output, BUF_LEN);
	if (ret <= 0) {
		fprintf(stderr, "[serialize map] error[%d].\n", ret);
	} else {
		fprintf(stderr, "[serialize map] %s.\n", output);
	}
///////////////////////////////////////////////////////////////////////////////////test serialize end

	return 0;
}
